<?php

class NestedPageSetList extends DBList {
	
	protected $dbTable = "tblNestedPageSet";
	protected $rowClassType = "NestedPageSet";
	
}

class NestedPageSet extends DBListItem {
	
	public function __construct($data=null, $parent=null) {
		parent::__construct($data);
		
		//Load NestedPages
		$t = new NestedPageList();
		$this->customdata["NestedPages"] = $t->getByFkNestedPageSet($this->pkNestedPageSet); 
	}
	
	/**
	 * Merges the NestedPageSet to the one given with parent
	 * Overrides NestedPages of the Parent with the ones in this PageSet if exist
	 *
	 * @param NestedPageSet $parent
	 */
	
	public function merge(NestedPageSet $parent) {
		
		// 1. Add all not defined NestedPages to this NestedPage
		// 2. Use the Layout of the ParentNestedPageSet if none is defined here
		
		
		/* Go trouh each of the parentNestedSet pages, and check 
		   if there exist a Page with this ID in the Current PageSet
		   if it doesn't exist copy the page of the parentPageSet 
		*/
		
		foreach ($parent->NestedPages as $pnp) {
			
			//Search an NestedPage
			$found = false;
			foreach ($this->NestedPages as $np) {
				if($pnp->ID == $np->ID) {
					$found = true;
					break;
				}
			}
			
			if(!$found) {
				
				// We've to add this NestedPage to us
				array_push($this->NestedPages, $pnp);
			}
		}
		
	}
	
}

class NestedPageList extends DBList {
	
	protected $dbTable = "tblNestedPage";
	protected $rowClassType = "NestedPage";

}

class NestedPage extends DBListItem {
	
	/**
	 * Loads the Referenced Page
	 * 
	 * @return Page
	 *
	 */
	function getPage() {
		
		//Create PageReuest
		$req = new PageRequest($this->fkPage, $this->method);
		
		//Load Page
		$page = PageFactory::CreatePage($this->fkPage);
		
		//Apply the PageRqeust to to Page for further use...
		$page->setPageRequest($req);
		
		return $page;
		
	}
	
}
	


?>